#include "registerwin.h"
#include "databasemanager.h"
extern DatabaseManager dbManager;
RegisterWin::RegisterWin(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("Register");

    setFixedSize(750, 600);
    QVBoxLayout *layout = new QVBoxLayout(this);

    usernameEdit = new QLineEdit(this);
    usernameEdit->setPlaceholderText("Username");
    usernameEdit->setMinimumHeight(70);

    QFont font;
    font.setPointSize(20);
    font.setBold(true);
    usernameEdit->setFont(font);
    layout->addWidget(usernameEdit);

    phoneEdit = new QLineEdit(this);
    phoneEdit->setPlaceholderText("Phone");
    phoneEdit->setMinimumHeight(70);
    phoneEdit->setFont(font);
    layout->addWidget(phoneEdit);

    passwordEdit = new QLineEdit(this);
    passwordEdit->setPlaceholderText("Password");
    passwordEdit->setEchoMode(QLineEdit::Password);
    passwordEdit->setMinimumHeight(70);
    passwordEdit->setFont(font);

    confirmPasswordEdit = new QLineEdit(this);
    confirmPasswordEdit->setPlaceholderText("Confirm Password");
    confirmPasswordEdit->setEchoMode(QLineEdit::Password);
    confirmPasswordEdit->setMinimumHeight(70);
    confirmPasswordEdit->setFont(font);

    QHBoxLayout *passwordLayout = new QHBoxLayout;
    QHBoxLayout *comfirmPasswordLayout = new QHBoxLayout;

    passwdEyeBtn = new QPushButton("Show", this);
    connect(passwdEyeBtn, &QPushButton::clicked, this, &RegisterWin::passwdShow);
    passwordLayout->addWidget(passwordEdit);
    passwordLayout->addWidget(passwdEyeBtn);
    passwdEyeBtn->setMinimumHeight(70);
    passwdEyeBtn->setFont(font);

    confirmEyeBtn = new QPushButton("Show", this);
    connect(confirmEyeBtn, &QPushButton::clicked, this, &RegisterWin::confirmPasswdShow);
    comfirmPasswordLayout->addWidget(confirmPasswordEdit);
    comfirmPasswordLayout->addWidget(confirmEyeBtn);
    confirmEyeBtn->setMinimumHeight(70);
    confirmEyeBtn->setFont(font);

    layout->addLayout(passwordLayout);
    layout->addLayout(comfirmPasswordLayout);
    QPushButton *registerButton = new QPushButton("Register", this);
    registerButton->setMinimumHeight(70);
    registerButton ->setFont(font);

    connect(registerButton, &QPushButton::clicked, this, &RegisterWin::onSubmit);
    layout->addWidget(registerButton);

    setLayout(layout);
}

void RegisterWin::onSubmit()
{
    QString username = usernameEdit->text();
    QString phone = phoneEdit->text();
    QString password = passwordEdit->text();
    QString confirmPassword = confirmPasswordEdit->text();

    // 1.用户名、手机号和密码不能为空
    if (username.isEmpty()  || phone.isEmpty()  || password.isEmpty())
    {
        QMessageBox::warning(this, "警告", "用户名、手机号和密码不能为空");
        return;
    }

    // 2.输入的用户名、手机号不能和已有用户冲突
    if(dbManager.usrExisted(username))
    {
        QMessageBox::warning(this, "警告", "用户名已存在 请更换");
        return;
    }

    if(dbManager.phoneExisted(phone))
    {
        QMessageBox::warning(this, "警告", "手机号已存在 请更换");
        return;
    }

    // 3.两次输入的密码不一致
    if (password != confirmPassword)
    {
        QMessageBox::warning(this, "警告", "两次输入的密码不一致");
        return;
    }

    // 4. 手机号 1开头+10数字
    QRegularExpression phoneRegex(R"(^1\d{10}$)");
    QRegularExpressionValidator *phoneValidator = new QRegularExpressionValidator(phoneRegex, this);
    phoneEdit->setValidator(phoneValidator);
    QRegularExpressionMatch match = phoneRegex.match(phone);

    if (!match.hasMatch())
    {
        // 如果手机号不符合要求 弹出警告框
        QMessageBox::warning(this, "Invalid Phone Number", "手机号需要1开头+10数字", QMessageBox::Ok);
        return;
    }

    // 5. 用户名不能以数字1开头
    if (username.startsWith('1'))
    {
        QMessageBox::warning(this, "警告", "用户名不能以数字1开头");
        return;
    }

    // 条件满足 添加用户
    if(dbManager.addNewUser(username,phone,password))
    {
        QMessageBox::information(this, "提示", "注册成功");
        // 关闭注册窗口 返回登录窗口
        accept();
    }
    else
    {
        QMessageBox::warning(this, "提示", "注册失败");
    }

}

void RegisterWin::passwdShow()
{
    // 切换密码框的显示模式
    if (passwordEdit->echoMode() == QLineEdit::Password)
        passwordEdit->setEchoMode(QLineEdit::Normal);
    else
        passwordEdit->setEchoMode(QLineEdit::Password); 
}

void RegisterWin::confirmPasswdShow()
{
    // 切换确认密码框的显示模式
    if (confirmPasswordEdit->echoMode() == QLineEdit::Password)
        confirmPasswordEdit->setEchoMode(QLineEdit::Normal);
    else
        confirmPasswordEdit->setEchoMode(QLineEdit::Password);
}

